package cn.com.inossem.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.inossem.activity.ProjectXiangmuNameActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.activity.UpdateProjectActivity;
import cn.com.inossem.model.ProjectXiangmuInfoBean;

public class ProjectXiangmuNameAdapter extends BaseAdapter {
	private ProjectXiangmuNameActivity mContext;
	private List<ProjectXiangmuInfoBean> list;

	public List<ImageView> listImage;
	public List<Integer> listFlag;
	public List<ProjectXiangmuInfoBean> Xuanzhongmessage;

	public boolean flag = false;

	private LayoutInflater mInflater;

	public ProjectXiangmuNameAdapter(ProjectXiangmuNameActivity context,
			List<ProjectXiangmuInfoBean> list,
			List<ProjectXiangmuInfoBean> Xuanzhongmessage) {
		this.mContext = context;
		this.list = list;
		this.Xuanzhongmessage = Xuanzhongmessage;
		Xuanzhongmessage = new ArrayList<ProjectXiangmuInfoBean>();
		listImage = new ArrayList<ImageView>();
		listFlag = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			listFlag.add(0);
		}
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public ProjectXiangmuInfoBean getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(
					R.layout.projectxiangmuname_item_layout, null);

			holder.llayout = (LinearLayout) convertView
					.findViewById(R.id.llayout);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.addcompany = (TextView) convertView
					.findViewById(R.id.addcompany);

			holder.choose = (ImageView) convertView.findViewById(R.id.choose1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		int length = list.size();

		if (position < length - 1) {
			holder.llayout.setVisibility(View.VISIBLE);
			holder.name.setText(list.get(position).getProjectName());
			holder.llayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					System.out.println("跳转下页！");
					Intent intent = new Intent(mContext,
							UpdateProjectActivity.class);

					ProjectXiangmuNameActivity.bean = list.get(position);
					// intent.putExtra("id", list.get(position).getId());
					// intent.putExtra("projectName", list.get(position)
					// .getProjectName());
					//
					// intent.putExtra("projectAddress", list.get(position)
					// .getProjectAddress());
					// intent.putExtra("projectNature", list.get(position)
					// .getProjectNature());
					// intent.putExtra("projectIndustry", list.get(position)
					// .getProjectIndustry());
					// intent.putExtra("projectModel", list.get(position)
					// .getProjectModel());
					// intent.putExtra("station",
					// list.get(position).getStation());
					// intent.putExtra("projectComment", list.get(position)
					// .getProjectComment());

					mContext.startActivity(intent);
				}
			});
			// holder.name.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// Intent intent = new Intent(mContext, InputActivity.class);
			// intent.putExtra("type", 2);
			// mContext.startActivityForResult(intent, 0);
			// }
			// });

		} else {
			holder.llayout.setVisibility(View.GONE);
			holder.addcompany.setVisibility(View.VISIBLE);
			holder.addcompany.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

//					 Intent intent = new Intent(mContext,
//					 InputActivity.class);
//					 intent.putExtra("position", position);
//					 intent.putExtra("type", 2);
//					 mContext.startActivityForResult(intent, 0);
					Intent intent = new Intent(mContext,
							UpdateProjectActivity.class);
					ProjectXiangmuInfoBean pb = new ProjectXiangmuInfoBean();
					pb.setProjectExpComID(mContext.comid);
					ProjectXiangmuNameActivity.bean = pb;

					mContext.startActivityForResult(intent, 0);
				}
			});
		}

		if (flag && position != list.size() - 1) {
			System.out.println("显示----" + position);
			holder.choose.setVisibility(View.VISIBLE);
		} else {
			System.out.println("隐藏----" + position);
			holder.choose.setVisibility(View.GONE);
		}
		if (getItem(position).getIsselected() == 1) {
			holder.choose.setBackgroundResource(R.drawable.xuanze);
			System.out.println("选中刷新:" + position);
		} else {
			holder.choose.setBackgroundResource(R.drawable.weixuanze);
			System.out.println("未选中刷新:" + position);
		}
		holder.choose.setTag(holder.choose);
		holder.choose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (getItem(position).getIsselected() == 1) {
					getItem(position).setIsselected(0);
					((ImageView) v.getTag())
							.setBackgroundResource(R.drawable.weixuanze);

					if (Xuanzhongmessage.contains(list.get(position))) {
						System.out.println("取消:" + position);
						Xuanzhongmessage.remove(list.get(position));
					}
				} else {
					getItem(position).setIsselected(1);
					((ImageView) v.getTag())
							.setBackgroundResource(R.drawable.xuanze);
					if (!Xuanzhongmessage.contains(list.get(position))) {
						System.out.println("选中:" + position);
						Xuanzhongmessage.add(list.get(position));
					}
				}
			}
		});

		// }
		return convertView;
	}

	public void showButton() {
		flag = true;
		this.notifyDataSetChanged();
		// System.out.println("listImage.size():" + listImage.size());
		// System.out.println("list.size():" + list.size());
		// for (int i = 0; i < listImage.size(); i++) {
		// listImage.get(i).setVisibility(View.VISIBLE);
		// }
	}

	public void hiddenButton() {
		flag = false;
		this.notifyDataSetChanged();
		// System.out.println("listImage.size():" + listImage.size());
		// System.out.println("list.size():" + list.size());
		// for (int i = 0; i < listImage.size(); i++) {
		// listImage.get(i).setVisibility(View.GONE);
		// }
	}

	public final class ViewHolder {
		public ImageView choose;
		public LinearLayout llayout;
		public TextView name;
		public TextView nametext;
		public TextView addcompany;
	}
}